#include <iostream>
#include <queue>
#include <stack>

using namespace std;

struct TreeNode {
    int val;
    TreeNode* left;
    TreeNode* right;

    TreeNode(int x) : val(x), left(NULL), right(NULL) {}
};

void recursivePreOrder(TreeNode* root) {
    if (root == NULL) {
        return;
    }

    cout << root->val << " ";
    recursivePreOrder(root->left);
    recursivePreOrder(root->right);
}

void iterativePreOrder(TreeNode* root) {
    if (root == NULL) {
        return;
    }

    stack<TreeNode*> s;
    s.push(root);

    while (!s.empty()) {
        TreeNode* node = s.top();
        s.pop();

        cout << node->val << " ";

        if (node->right) {
            s.push(node->right);
        }
        if (node->left) {
            s.push(node->left);
        }
    }
}

void recursiveInOrder(TreeNode* root) {
    if (root == NULL) {
        return;
    }

    recursiveInOrder(root->left);
    cout << root->val << " ";
    recursiveInOrder(root->right);
}

void iterativeInOrder(TreeNode* root) {
    if (root == NULL) {
        return;
    }

    stack<TreeNode*> s;
    TreeNode* current = root;

    while (current != NULL || !s.empty()) {
        while (current != NULL) {
            s.push(current);
            current = current->left;
        }

        current = s.top();
        s.pop();

        cout << current->val << " ";

        current = current->right;
    }
}

void recursivePostOrder(TreeNode* root) {
    if (root == NULL) {
        return;
    }

    recursivePostOrder(root->left);
    recursivePostOrder(root->right);
    cout << root->val << " ";
}

void iterativePostOrder(TreeNode* root) {
    if (root == NULL) {
        return;
    }

    stack<TreeNode*> s1, s2;
    s1.push(root);

    while (!s1.empty()) {
        TreeNode* node = s1.top();
        s1.pop();
        s2.push(node);

        if (node->left) {
            s1.push(node->left);
        }
        if (node->right) {
            s1.push(node->right);
        }
    }

    while (!s2.empty()) {
        cout << s2.top()->val << " ";
        s2.pop();
    }
}

void levelOrderTraversal(TreeNode* root) {
    if (root == NULL) {
        return;
    }

    queue<TreeNode*> q;
    q.push(root);

    while (!q.empty()) {
        TreeNode* node = q.front();
        q.pop();

        cout << node->val << " ";

        if (node->left) {
            q.push(node->left);
        }
        if (node->right) {
            q.push(node->right);
        }
    }
}

int main() {
    TreeNode* root = new TreeNode(1);
    root->left = new TreeNode(2);
    root->right = new TreeNode(3);
    root->left->left = new TreeNode(4);
    root->left->right = new TreeNode(5);

    cout << "递归先序遍历：";
    recursivePreOrder(root);
    cout << endl;

    cout << "非递归先序遍历：";
    iterativePreOrder(root);
    cout << endl;

    cout << "递归中序遍历：";
    recursiveInOrder(root);
    cout << endl;

    cout << "非递归中序遍历：";
    iterativeInOrder(root);
    cout << endl;

    cout << "递归后序遍历：";
    recursivePostOrder(root);
    cout << endl;

    cout << "非递归后序遍历：";
    iterativePostOrder(root);
    cout << endl;

    cout << "层序遍历：";
    levelOrderTraversal(root);
    cout << endl;

    return 0;
}